﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 整数反转
{
    class Program
    {
        static void Main(string[] args)
        {

            int test = -214748368;
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            Solution solution = new Solution();
            //Console.WriteLine(solution.Reverse(test));
            Console.WriteLine(solution.reverse(test));
            stopwatch.Stop();
            Console.WriteLine(stopwatch.ElapsedMilliseconds + "ms");
            Console.ReadLine();
        }
    }

    public class Solution
    {
        public int Reverse(int x)
        {
            long result = 0;
            if (x > int.MaxValue || x < int.MinValue)
            {
                result = 0;
            }
            else
            {
                if (x > 0)
                {
                    string a = x.ToString();
                    StringBuilder b = new StringBuilder(); ;
                    for (int i = a.Length - 1; i >= 0; i--)
                    {
                        b.Append(a[i]);
                    }
                    result = long.Parse(b.ToString());
                }
                else if (x < 0)
                {
                    string a = x.ToString();
                    StringBuilder b = new StringBuilder(); ;
                    for (int i = a.Length - 1; i > 0; i--)
                    {
                        b.Append(a[i]);
                    }
                    result = -long.Parse(b.ToString());
                }
                else
                {
                    result = 0;
                }
          
            }
            if (result > int.MaxValue || result < int.MinValue)
            {
                result = 0;
            }
            return (int)result;
        }

        public int reverse(int x)
        {
            long n = 0;
            while (x != 0)
            {
                n = n * 10 + x % 10;
                x = x / 10;
            }
            return (int)n == n ? (int)n : 0;
        }
    }
}
